using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Microsoft.SharePoint.WebControls;

using CoolStuffs.Sharepoint.Webparts.RecentItems;
using CoolStuffs.Sharepoint.Webparts.RecentItems.Common;
using CoolStuffs.Sharepoint.Webparts.RecentItems.Support;

namespace CoolStuffs.Sharepoint.Webparts.RecentItems.Pages
{
    public partial class RecentDocuments : Microsoft.SharePoint.WebControls.LayoutsPageBase
    {

        #region Member Variables

        #region Protected
        /// <summary>
        /// the webpart instance on the static page
        /// NOTE : webpart ID in linear registry format
        /// </summary>
        protected DocumentsAggregator g_e451ebf7_c356_4408_ba41_9a7cbb006ce0;

        /// <summary>
        /// error label control
        /// </summary>
        protected Label uxErrorLabel;

        /// <summary>
        /// panel in which
        /// the webpart is placed
        /// </summary>
        protected Panel uxWebpartDisplayPanel;
        #endregion

        #region Private
        /// <summary>
        /// 
        /// </summary>
        private Dictionary<string, string> _queryStringCollection;

        /// <summary>
        /// 
        /// </summary>
        private const System.String _hide = "Hide";

        /// <summary>
        /// 
        /// </summary>
        private const System.String _show = "Show";

        /// <summary>
        /// 
        /// </summary>
        private DocumentsAggregator _documentAggregatorInstance;
        #endregion

        #endregion

        #region Properties

        #region Public
        /// <summary>
        /// Get/Set Webpart instance
        /// </summary>
        public DocumentsAggregator DocumentAggregatorWebpart
        {
            get
            {
                return this._documentAggregatorInstance;
            }
            set
            {
                this._documentAggregatorInstance = value;
            }
        }
        #endregion

        #region Protected
        /// <summary>
        /// query string name-value collection to be
        /// serialized in controlstate
        /// </summary>
        protected Dictionary<String, String> QueryStringCollection
        {
            get
            {
                return this._queryStringCollection;
            }
            set
            {
                this._queryStringCollection = value;
            }
        }
        #endregion

       #endregion

        #region Methods

        #region Protected

        /// <summary>
        /// OnInit handler of the page
        /// </summary>
        /// <param name="e">EventArgs</param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this._queryStringCollection = new Dictionary<string, string>();
            this._queryStringCollection.Add(Constants.ItemLimitProperty, string.Empty);
            this._queryStringCollection.Add(Constants.GroupByProperty, string.Empty);
            this._queryStringCollection.Add(Constants.PageSizeProperty, string.Empty);
            this._queryStringCollection.Add(Constants.SearchScopeProperty, string.Empty);
            this._queryStringCollection.Add(Constants.ViewCreatedOnProperty, string.Empty);
            this._queryStringCollection.Add(Constants.ViewItemNameProperty, string.Empty);
            this._queryStringCollection.Add(Constants.ViewListNameProperty, string.Empty);
            this._queryStringCollection.Add(Constants.ViewModifiedByProperty, string.Empty);
            this._queryStringCollection.Add(Constants.ViewModifiedOnProperty, string.Empty);
            this._queryStringCollection.Add(Constants.ViewSiteNameProperty, string.Empty);
            this.RegisterRequiresControlState(this);
        }

        /// <summary>
        /// Loading the Control State
        /// </summary>
        /// <param name="savedState">retrive the saved state in previous post back</param>
        protected override void LoadControlState(object savedState)
        {
            base.LoadControlState(savedState);
            this.QueryStringCollection = savedState as Dictionary<String, String>;
            this.setWebpartProperties();
        }

        /// <summary>
        /// SaveControlState Event handler
        /// </summary>
        /// <returns>saved srializable object</returns>
        protected override object SaveControlState()
        {
            return this.QueryStringCollection;
        }

        /// <summary>
        /// OnLoad event of the page
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.setWebpartProperties();
        }

        #endregion

        #region Private

        /// <summary>
        /// loads the dictionary object from
        ///  the query string for the first time
        /// </summary>
        private void loadDictionaryFromQueryString()
        {
            this.loadDictionary(Constants.ItemLimitProperty);
            this.loadDictionary(Constants.PageSizeProperty);
            this.loadDictionary(Constants.ViewCreatedOnProperty);
            this.loadDictionary(Constants.ViewModifiedOnProperty);
            this.loadDictionary(Constants.ViewListNameProperty);
            this.loadDictionary(Constants.ViewSiteNameProperty);
            this.loadDictionary(Constants.ViewModifiedByProperty);
            this.loadDictionary(Constants.GroupByProperty);
            this.loadDictionary(Constants.SearchScopeProperty);
        }

        /// <summary>
        /// set the properties of the webpart retrived from
        /// query string
        /// </summary>
        private void setWebpartProperties()
        {
            try
            {
                this.loadDictionaryFromQueryString();
                this.g_e451ebf7_c356_4408_ba41_9a7cbb006ce0.ItemLimit =
                    Convert.ToInt32(this.QueryStringCollection[Constants.ItemLimitProperty]);
                this.g_e451ebf7_c356_4408_ba41_9a7cbb006ce0.PageSize =
                    Convert.ToInt32(this.QueryStringCollection[Constants.PageSizeProperty]);
                this.g_e451ebf7_c356_4408_ba41_9a7cbb006ce0.ViewCreatedOnColumn =
                    Convert.ToBoolean(this.QueryStringCollection[Constants.ViewCreatedOnProperty]);
                this.g_e451ebf7_c356_4408_ba41_9a7cbb006ce0.ViewModifiedOnColumn =
                    Convert.ToBoolean(this.QueryStringCollection[Constants.ViewModifiedOnProperty]);
                this.g_e451ebf7_c356_4408_ba41_9a7cbb006ce0.ViewListNameColumn =
                    Convert.ToBoolean(this.QueryStringCollection[Constants.ViewListNameProperty]);
                this.g_e451ebf7_c356_4408_ba41_9a7cbb006ce0.ViewSiteNameColumn =
                    Convert.ToBoolean(this.QueryStringCollection[Constants.ViewSiteNameProperty]);
                this.g_e451ebf7_c356_4408_ba41_9a7cbb006ce0.ViewModifiedByColumn =
                    Convert.ToBoolean(this.QueryStringCollection[Constants.ViewModifiedByProperty]);
                this.g_e451ebf7_c356_4408_ba41_9a7cbb006ce0.SearchOptions =
                    this.getSearchOption(this.QueryStringCollection[Constants.SearchScopeProperty]);
                this.g_e451ebf7_c356_4408_ba41_9a7cbb006ce0.GroupBy =
                    this.getGroupByField(this.QueryStringCollection[Constants.GroupByProperty]);
            }
            catch (Exception ex)
            {
                this.uxWebpartDisplayPanel.Visible = false;
                this.uxErrorLabel.Text = Constants.ErrorMessage;
            }
        }

        /// <summary>
        /// get the valid groupby property
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private BaseContentAggregator.GroupByScope getGroupByField(string value)
        {
            try
            {
                return (BaseContentAggregator.GroupByScope)
                    Enum.Parse(typeof(BaseContentAggregator.GroupByScope), value);
            }
            catch
            {
                return BaseContentAggregator.GroupByScope.None;
            }
        }

        /// <summary>
        /// get the valid search option 
        /// enum values 
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private BaseContentAggregator.SearchScope getSearchOption(string value)
        {
            try
            {
                return (BaseContentAggregator.SearchScope)
                    Enum.Parse(typeof(BaseContentAggregator.SearchScope), value);
            }
            catch
            {
                return BaseContentAggregator.SearchScope.None;
            }
        }

        /// <summary>
        /// loads the dictionary if this
        /// is the first time call and the dictionary
        /// is not saved in control state
        /// </summary>
        /// <param name="key">value for this key be stored</param>
        private void loadDictionary(string key)
        {
            if (this.Request.QueryString[key] != null &&
                string.IsNullOrEmpty(this.QueryStringCollection[key]))
            {
                this.QueryStringCollection[key] = this.Request.QueryString[key];
            }
        }

        #endregion

        #endregion
    }
}
